package com.dst.smart.receiver;

import com.dst.smart.app.MainActivity;
import com.dst.smart.app.MyApplication;
import com.dst.smart.handler.entity.ServiceType;
import com.dst.smart.json.ProjectUtils;
import com.dst.smart.service.SendCMD;
import com.dst.smart.service.TcpSender;
import com.dst.smart.service.SendThread;
import com.dst.smart.socket.SocketClient;
import com.dst.smart.socket.info.Command;
import com.dst.smart.socket.info.ForWardInfo;
import com.dst.smart.tools.AppTools;
import com.dst.smart.tools.JSONHelper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import static android.graphics.drawable.GradientDrawable.LINE;

public class ReceiverBroadcast extends BroadcastReceiver {
	public static  int isNet=0;
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String result = intent.getExtras().getString("str");
		try {
			Command info = JSONHelper
					.parseObject(result, Command.class);
			if (info != null) {
				String service = info.getCommand();
				if (!TextUtils.isEmpty(service)) {
					ServiceType serviceType = ServiceType
							.getServiceType(service);
					switch (serviceType) {
					case LOGIN:
						if(isNet==0) {
							MainActivity.playSound(4, AppTools.getServiceName());
							isNet=1;
						}
						SocketClient.isLinkFlag = 120;
						SocketClient.plusNumber = 30;
						break;
					case LINE:
						SocketClient.isLinkFlag = 120;
						SocketClient.plusNumber = 30;
						break;
					case CONNECT:
						if(info!=null){
							Log.e("domtao",info.getToId()+"不在线");
						}
						break;
					case FORWARD:
						if (info != null) {
							try {
								String code = info.getData();
								if (code.equals("*SB01C00000E")) {
									TcpSender.deviceID = info.getToId();
								}
								new SendThread();
								SendThread thread = SendThread.getInstance((info.getType()==1)?1:2,code);
								new Thread(thread).start();
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
						break;
					case FORWARDMASTERTINFO:
						if (info != null) {
							String toID = info.getToId();
							String msg = info.getData();
							ResponseActionInfo.getMasterInfo(toID, msg);
						}

						break;
					case FORWARDUPDATEMASTERTINFO:

						if (info != null) {
							String toID = info.getToId();
							String msg = info.getData();
							ResponseActionInfo.setMasterInfo(toID, msg);
						}
						break;
					case FORWARDADDARCE:
						if (info != null) {
							String toID = info.getToId();
							String msg = info.getData();
							ResponseActionInfo.addArce(toID, msg);
						}

						break;
					case FORWARDADDDEVICE:
						if (info != null) {
							String toID = info.getToId();
							String msg = info.getData();
							ResponseActionInfo.addDevice(toID, msg);
						}
						break;
					case FORWARDADDANHONG:
						if (info != null) {
							String toID = info.getToId();
							String msg = info.getData();
							ResponseActionInfo.addAnHong(toID, msg);
						}
						break;
					case FORWARDUPDATEVERSION:
						if (info != null) {
							int msg = Integer.parseInt(info.getData());
							switch (msg) {
							case 0:
								ProjectUtils.getRootPath().setIsSpeech(0);
								ProjectUtils.saveRootPath();
								MainActivity.playSound(5, "打开语音播报");
								break;
							case 1:
								ProjectUtils.getRootPath().setIsSpeech(1);
								ProjectUtils.saveRootPath();
								MainActivity.playSound(5, "关闭语音播报");
								break;
							default:
								break;
							}

						}
						break;
					case FORWARDADDMODE:
						if (info != null) {
							String toID = info.getToId();
							String msg = info.getData();
							ResponseActionInfo.addMode(toID, msg);
						}
						break;
					case FORWARDADDVOICE:
						if (info != null) {
							String msg = info.getData();
							ResponseActionInfo.addVoice(msg);
						}
						break;
					case FORWARDVOICEINFO:
						if (info != null) {
							String toID = info.getToId();
							ResponseActionInfo.getVoiceInfo(toID);
						}

						break;
					case FORWARDDElVOICE:
						if (info != null) {
							ResponseActionInfo.delVoice(info.getData());
						}
						break;

					case FORWARDADDSUBSCRIBEINFO:
						if (info != null) {
							String msg = info.getData();
							ResponseActionInfo.addSubScribe(msg);
						}
						break;
					case FORWARDSUBSCRIBEINFO:
						if (info != null) {
							String toID = info.getToId();
							ResponseActionInfo.getSubscribeInfo(toID);
						}

						break;
					case FORWARDDELSUBSCRIBEINFO:
						if (info != null) {
							ResponseActionInfo.delSubscribe(info.getData());
						}
						break;
					case FORWARDVERSIONAPK:
					if (info != null) {
							String toID = info.getToId();
							ResponseActionInfo.getApkVersion(toID,
									info.getData());
						}
						break;
					case FORWARDUPDATEAPK:
						if (info != null) {
							String toID = info.getToId();
							ResponseActionInfo.updateApk(toID,
									info.getData());

						}
						break;

					case FORWARDADDREDINFRA:
						if (info != null) {
							String toID = info.getToId();
							String msg = info.getData();
							ResponseActionInfo.addREdInfra(toID, msg);
						}
						break;
					case FORWARDADDMODELIST:
						if (info != null) {
							String toID = info.getToId();
							String msg = info.getData();
							ResponseActionInfo.addModeList(toID, msg);
						}
						break;
					case FORWARDDELDEVICE:
						if (info != null) {
							String msg = info.getData();
							ResponseActionInfo.setDeleteDevice(msg);
						}
						break;

					case FORWARDDELMODE:
						if (info != null) {
							String msg = info.getData();
							ResponseActionInfo.setDeleteMode(msg);
						}
						break;
						case FORWARDDELMODELIST:
							if (info != null) {
								String msg = info.getData();
								ResponseActionInfo.setDeleteModeList(msg);
							}
							break;

					case FORWARDNAMECONTROL:
						if (info != null) {
							String msg = info.getData();
							ResponseActionInfo.setNameControl(msg);
						}
						break;
					case FORWARDGETDEVICE:
						if (info != null) {
							String toID = info.getToId();
							String msg = info.getData();
							ResponseActionInfo.getDevice(toID, msg);
						}
						break;
					case FORWARDGETMODE:
						if (info != null) {
							String toID = info.getToId();
							String msg = info.getData();
							ResponseActionInfo.getMode(toID, msg);
						}
						break;
					case FORWARDGETMODELIST:
						if (info != null) {
							String toID = info.getToId();

							String msg = info.getData();
							ResponseActionInfo.getModeList(toID, msg);
						}
						break;
					case FORWARDGETANHONG:
						if (info != null) {
							String toID = info.getToId();
							String msg = info.getData();
							ResponseActionInfo.getAnHong(toID, msg);
						}
						break;

					case FORWARDUPDATEEVICE:
						if (info != null) {
							String msg = info.getData();
							ResponseActionInfo.updateDevice(msg);
						}
						break;
					case FORWARDSONG:
						if (info != null) {
							String msg = info.getData();
							ResponseActionInfo.updateSong(msg);
						}

						break;
					case FORWARDUPDATEMODE:
						if (info != null) {
							String msg = info.getData();
							ResponseActionInfo.updateMode(msg);
						}
						break;
					case FORWARDUPDATEANHONG:
						if (info != null) {
							String msg = info.getData().replace("，", ",");
							ResponseActionInfo.updateAnHong(msg);
						}
						break;
					case FORWARDDELALLDEVICE:
						if (info != null) {
							String msg = info.getData();
							if (msg.equals("AllDown")) {
								MyApplication.getInstance().isDown = 1;
								MainActivity.playSound(5, "开始下载数据！");
							} else if (msg.equals("CancleDown")) {
								MyApplication.getInstance().isDown = 0;
								MainActivity.playSound(5, "下载成功！");

							} else {
								ResponseActionInfo.setDeleteAllDevice(msg);
							}
						}
						break;
					case FORWARDDELALLVOICE:
						if (info != null) {
							String msg = info.getData();
							ResponseActionInfo.setDeleteAllVoice(msg);
						}
						break;
						case FORWARDGATEWAY:
							if (info != null) {
								String toID = info.getToId();
								String[] msg = info.getData().trim().split(":");
								if(msg.length==2){
									if(msg[0].equals("getDevice")) {
										ResponseActionInfo.getGateWay(toID);
									}else if(msg[0].equals("add")) {
										ResponseActionInfo.addGateWay(msg[1]);
									}else if(msg[0].equals("update")) {
										ResponseActionInfo.updateGateWay(msg[1]);
									}else if(msg[0].equals("delete")) {
										ResponseActionInfo.deleteGateWay(msg[1]);
									}else if(msg[0].equals("")) {

									}
								}
							}
							break;

					}
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
